package cn.itsource.smartcampus.service.impl;

import cn.itsource.smartcampus.domain.Student;
import cn.itsource.smartcampus.domain.Teacher;
import cn.itsource.smartcampus.mapper.StudentMapper;
import cn.itsource.smartcampus.service.StudentService;
import cn.itsource.smartcampus.util.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;
import java.util.List;
@Service
/*针对只读的事务*/
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper SMapper;
    @Override
    public Student selectByPrimaryKey(Long id) {
     return SMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Student> getAll() {
        return SMapper.getAll();
    }

    @Override
    @Transactional
    public int deleteByPrimaryKey(Long id) {
        return SMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public int insert(Student stu) {
        return SMapper.insert(stu);
    }

    @Override
    @Transactional
    public int updateByPrimaryKeySelective(Student stu) {
        return SMapper.updateByPrimaryKeySelective(stu);
    }

    /**
     * 导入文件，然后批量添加文件里的数据
     * @param file
     */
    @Override
    public void importExcel(MultipartFile file) {
        try {
            // 1.使用工具类从复杂文件中获取到老师信息集合
            List<Student> students = ExcelUtil.importExcel(file, Student.class);
            // 2.调用批量新增方法，保存所有老师信息
            SMapper.batchAdd(students);
        } catch (IOException e) {
            throw new RuntimeException(e);

        }
    }
}
